-----------------------------------------------------------
--- /shared/xml/xml_node.lua                            ---
--- Part of openFrame project                           ---
--- Written by 50p. Additional changes by Orange.       ---
--- Lately edited in revision number 13 by Orange       ---
--- Licensed under BSD licence                          ---
-----------------------------------------------------------

-- Class: XMLNode Class
-- Node extension of XML class.
XMLNode = of.class:new()

--	Function: XMLNode:new
--	Private extending function. Used to create Node objects.
--	
--	Required Arguments:
--	   * xmlnode: (xmlnode) Node to create an object from it
--	   
--	Returns:
--		The result is a XMLNode object
function XMLNode:new( xmlnode )

	--local atrs = xmlNodeGetAttributes( xmlnode );
	--print( atrs[ "posX" ], xmlNodeGetName( xmlnode ) )

	local node = of.class:new(self)
	
	local ParseNodes = function( nodesTable )
		if ( nodesTable and type( nodesTable ) == "table" ) and #nodesTable > 0 then
			local nodes = { };
			for i = 1, #nodesTable do
				table.insert( nodes, XMLNode:new( nodesTable[ i ] ) );
			end
			return nodes;
		end
		return nil;
	end	
	
	
	if ( type( xmlnode ) == "userdata" ) then
		node.Name = xmlNodeGetName( xmlnode );
		node.Value = xmlNodeGetValue( xmlnode );
		node.Attributes = { };
		local attrbts = xmlNodeGetAttributes( xmlnode );
		if ( attrbts and table.size( attrbts ) > 0 ) then
			for name, value in pairs( attrbts ) do
				node.Attributes[ name ] = value;
			end
		end
		node.Nodes = ParseNodes( xmlNodeGetChildren( xmlnode ) );
	end
	
	return node;
end

--	Function: XMLNode:Attribute
--	Sets or gets specified attribute of node
--	
--	Required Arguments:
--	   * name: (string) Name of attribute to get/set
--	   
--  Optional Arguments:
--	   * value: (string) If set, the attribute value will be set to it.
--
--	Returns:
--		* Returns string with attribute's value if just name was specified
--      * Returns true if the attribute was successfully set (when value specified)
--      * Returns false if something was wrong
function XMLNode:Attribute( name, value )
	if not self then wrongClassUsageMessage( "xmlNode", "Attribute" ); return end

	if ( name ) then
		if ( value ) then
			self.Attributes[ name ] = tostring( value );
			return true;
		else
			return self.Attributes[ name ];
		end
	end
	return false;
end

--	Function: XMLNode:Name
--	Sets or gets name of node
--	
--	Optional Arguments:
--	   * newname: (string) New name of node
--	   
--	Returns:
--		* Returns string with name if newname wasn't specified
--      * Returns true if the name was successfully set (when newname specified)
--      * Returns false if something was wrong
function XMLNode:Name( newname )
	if not self then wrongClassUsageMessage( "xmlNode", "Name" ); return end
	
	if ( newname ) then
		self.name = newname;
		return true;
	else
		return self.name;
	end
end

--	Function: XMLNode:Value
--	Sets or gets value of node
--	
--	Optional Arguments:
--	   * newvalue: (string) New value of node
--	   
--	Returns:
--		* Returns string with node's value if newname wasn't specified
--      * Returns true if the value was successfully set (when newvalue specified)
--      * Returns false if something was wrong
function XMLNode:Value( newvalue )
	if not self then wrongClassUsageMessage( "xmlNode", "Value" ); return end
	
	if ( newname ) then
		self.value = newvalue;
		return true;
	else
		return self.value;
	end
end

